<?PHP
	$path = 'C:/Casereporting/';
	$string=$_POST['txtFreetext'];
	if (file_exists($path)) 
	{
	createYear();
	}
	else
	{
	createCaseFolder();		
	}

	function writeWord()
	{
		$CSfolder='C:/Casereporting/';
		$CurYear='20'.date("y",time());
		$CurMont='/'.date("m",time()).'/';
		$path = $CSfolder.$CurYear.$CurMont;
		$line="";
		if (file_exists($path.'testing.doc'))
		{	
			$fh = fopen($path."testing.doc", "r");
			while (!feof($fh)) 
			{
				$line = $line.fgets($fh);
				$f=$path."testing.doc";				
			}
			$fp = fopen($f, 'wb');

			$constants=str_replace("PHP","PHP1",$line."\n".$_POST['txtFreetext']);
			fwrite($fp, $constants);
			fclose($fp);
		}
		else
		{
		
		$f=$path."testing.doc";
		$fp = fopen($f, 'wb');

		$constants=str_replace("PHP","PHP1",$_POST['txtFreetext']);
		fwrite($fp, $constants);
		fclose($fp);
		}		
	}	
	
	function createMonth()	
	{
	$CSfolder='C:/Casereporting/';
	$CurYear='20'.date("y",time());
	$CurMont='/'.date("m",time()).'/';
	$path = $CSfolder.$CurYear.$CurMont;
	if (file_exists($path))
	{
	writeWord();
	}
	else
	{
	mkdir("$path", 0777);
	writeWord();
	}
	}
	
	function createYear()	
	{
		$CSfolder='C:/Casereporting/';
		$CurYear='20'.date("y",time());
		$path = $CSfolder.$CurYear;
		if (file_exists($path))
		{
			createMonth();
		}
		else
		{
		mkdir("$path", 0777);
		$year  =date("y",time());
		//The 20 is for the Millennium 
		$path=$path.'20'.$year.'/';
		createMonth();
		}
	}
	function createCaseFolder()	
	{	$CSfolder='C:/Casereporting/';
		$path = $CSfolder;
		mkdir("$path", 0777);
		createYear();
	}
?>